package com.jhte.scheduleJob.base.util;

import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

public class PropertiesUtils {

    public static void main(String[] args) {
        String p = PropertiesUtils.getProperty("websocketUrl");
        System.out.println(p);
        PropertiesUtils.getProperty("websocketUrl", "property", "hh");
        PropertiesUtils.getProperty("websocketUrl1", "property", "hh");

    }

    /**
     * 获取配置文件   property.properties key对应的value
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        try {
            Properties props = new Properties();
            props = PropertiesLoaderUtils.loadAllProperties("property.properties");
            String value = props.getProperty(key);
            System.out.println(key + ":" + value);
            return value;
        } catch (IOException e) {
            System.out.println("PropertiesUtils.getProperty" + e.getMessage());
            return null;
        }
    }

    /**
     * 获取配置文件   "source".properties key对应的value
     *
     * @param key
     * @param source       文件名
     * @param defaultValue 默认返回值
     * @return
     */
    public static String getProperty(String key, String source, String defaultValue) {
        try {
            Properties props = new Properties();
            props = PropertiesLoaderUtils.loadAllProperties(source + ".properties");
            String value = props.getProperty(key, defaultValue);
            System.out.println(key + ":" + value);
            return value;
        } catch (IOException e) {
            System.out.println("PropertiesUtils.getProperty" + e.getMessage());
            return defaultValue;
        }
    }

    /**
     * 获取配置文件   "source".properties key对应的value
     *
     * @param key
     * @param source 文件名
     * @return
     */
    public static String getProperty(String key, String source) {
        try {
            Properties props = new Properties();
            props = PropertiesLoaderUtils.loadAllProperties(source + ".properties");
            String value = props.getProperty(key);
            System.out.println(key + ":" + value);
            return value;
        } catch (IOException e) {
            System.out.println("PropertiesUtils.getProperty" + e.getMessage());
            return null;
        }
    }

}
